﻿using System;
using AngryBullets.Input;
using AngryBullets.Sprite;
using AngryBullets.Sprite.Object;
using AngryBullets.Scenes.Core;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace AngryBullets.Scenes
{
    public class GameplayScene : AbstractGameScene
    {
        #region Fields

        private ContentManager _content;
        private SpriteFont _gameFont;
        private float _pauseAlpha;
        private Map map;


        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public GameplayScene(SceneManager sceneMgr)
            : base(sceneMgr)
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            map = new Map("map.txt");
        }

        protected override void LoadContent()
        {
            if (_content == null)
                _content = new ContentManager(SceneManager.Game.Services, "Content");

            _gameFont = _content.Load<SpriteFont>("gamefont");

            SceneManager.Game.ResetElapsedTime();

            foreach (Sprite.Sprite obj in Map.MapObjects)
            {
                obj.LoadContent(_content);
            }

            foreach (Sprite.Object.AnimationSprite obj in Map.MapAnime)
            {
                obj.LoadContent(_content);
            }
        }

        protected override void UnloadContent()
        {
            _content.Unload();
        }

        #endregion

        #region Update and Draw

        public override void Update(GameTime gameTime, bool othersceneHasFocus, bool coveredByOtherscene)
        {
            base.Update(gameTime, othersceneHasFocus, false);

            _pauseAlpha = coveredByOtherscene
                ? Math.Min(_pauseAlpha + 1f / 32, 1)
                : Math.Max(_pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                foreach (AnimationSprite obj in Map.MapAnime)
                {
                    obj.Update(gameTime);
                }

                foreach (Baliste baliste in Map.MapAnime)
                {
                    baliste.Move(gameTime);
                }
            }
        }


        public override void Draw(GameTime gameTime)
        {
            SceneManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Orange, 0, 0);
            SpriteBatch spriteBatch = SceneManager.SpriteBatch;
            spriteBatch.Begin();

            foreach (Sprite.Sprite obj in Map.MapObjects)
            {
                obj.Draw(spriteBatch);
            }
            foreach (AnimationSprite obj in Map.MapAnime)
            {
                obj.Draw(spriteBatch);
            }

            spriteBatch.End();

            if (TransitionPosition > 0 || _pauseAlpha > 0)
            {
                float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, _pauseAlpha / 2);
                SceneManager.FadeBackBufferToBlack(alpha);
            }
        }

        #endregion
    }
}
